$(function () {
    $("#right1").show();
    $("#right2").hide();

    $('#btn1').click(function () {
        var account = $('#form1 input[name="account"]').val();
        var pwd = $('#form1 input[name="pwd"]').val();
        var func='login';
        var data={
            'func':func,
            'account':account,
            'pwd':pwd
        };
        if(account==""||pwd=="")
            alert("账号和密码不能为空。")
        // var url="http://127.0.0.1:5000/login"
        else{
            $.ajax({
                url: "/store/login",
                type: 'post',
                data: data,
                success: function (data) {
                    if(data['type']=='customer')
                        location.href="/store/customer/"+data['account'];
                    else if(data['type']=='waiter')
                        location.href="/store/waiter/"+data['account'];
                    else if(data['type']=='host')
                        location.href="/store/host/"+data['account'];
                    else {
                        // 错误输出
                        $('#error1').html(data['info']);
                    }
                    // console.log(data)
                },
                error: function (error) {
                    console.log(error)
                }
            });
        }
    });

    $(document).keydown(function(event){
        if(event.keyCode==13){
            $('#btn1').click();
        }
    });

    $('#btn2').click(function () {
        $("#right2").show();
        $("#right1").hide();
    });

    $('#btn3').click(function () {
        $("#right1").show();
        $("#right2").hide();
    });

    $('#btn4').click(function () {
        var account = $('#form2 input[name="account"]').val();
        var pwd = $('#form2 input[name="pwd"]').val();
        var pwdAffirm = $('#form2 input[name="pwdAffirm"]').val();
        var func='register';
        var data={
            'func':func,
            'account':account,
            'pwd':pwd,
            'pwdAffirm':pwdAffirm
        };
        // var url="http://127.0.0.1:5000/login"
        if(account==""||pwd==""||pwdAffirm=="")
            alert("不允许有空信息被提交。");
        else {
            $.ajax({
                url: "/store/login",
                type: 'post',
                data: data,
                success: function (data) {
                    if (data['type'] == 'error')
                        $('#error2').html(data['info']);
                    else {
                        // 跳转
                        // location.href="http://127.0.0.1:5000/success-register";
                        alert("注册成功！请使用注册好的账号、密码登录。")
                        location.href="/store/login"
                    }
                    // console.log(data)
                },
                error: function (error) {
                    console.log(error)
                }
            });
        }
    });
});
